from django.shortcuts import render,HttpResponse
import json
from web import models
# Create your views here.
def index(request):
    if request.method == 'GET':
        return render(request,'asset.html')
    elif request.method == 'POST':
        content = request.POST['data']
        data={}
        ret={}
        update=0
        for row in json.loads(content):
           for k,v in row.items():
               if k == 'id':
                   data_id=v
               elif k == 'device_status_choices':
                   data['device_status_id']=v
               elif k == 'idc_choices':
                   data['idc_id']=v
               elif k == 'cabinet_order':
                   data['cabinet_order']=v
           try:
               models.Asset.objects.filter(id=data_id).update(**data)

           except Exception as e:
               print('更新数据库出错')
               update=1
        if update == 1:
            ret['status']='更新过程出现错误'
        else:
            ret['status']='更新数据成功'
        return HttpResponse(json.dumps(ret,ensure_ascii=False))

def get_data(request):
    table_config=[
         {
                'q': None,
                'title': "选项",
                'display': True,
                'text': {'content': "{n}","kwargs": {'n':'<input type=checkbox />'}},
                'attrs': {}
            },
            {
                'q': 'id',
                'title': 'ID',
                'display': False,
                'text':{},
                'attrs': {}
            },
            {
                'q': 'device_type_id',
                'title': '资产类型',
                'display': True,
                'text': {'content': "{n}", 'kwargs': {'n': "@@device_type_choices"}},
                'attrs': {}
            },
            {
                'q': 'device_status_id',
                'title': '状态',
                'display': True,
                'text': {'content': "{n}", 'kwargs': {'n': "@@device_status_choices"}},
                'attrs': {'name':'device_status_id','origin':"@device_status_id",'edit-enable': 'true', 'edit-type': 'select',"global-name": 'device_status_choices'}
            },
            {
                'q': 'idc__id',
                'title': 'IDC',
                'display': False,
                'text': {},
                'attrs': {}
            },
            {
                'q': 'idc__name',
                'title': 'IDC',
                'display': True,
                'text': {'content': "{n}", 'kwargs': {'n': "@idc__name"}},
                'attrs': {'name':'idc_id','origin':"@idc__id",'edit-enable': 'true', 'edit-type': 'select',"global-name": 'idc_choices'}
            },
            {
                'q': 'cabinet_order',
                'title': '机柜位置',
                'display': True,
                'text': {'content': "{n}",'kwargs': {'n': "@cabinet_order"}},
                'attrs': {'name':'cabinet_order','origin':"@cabinet_order",'edit-enable': 'true', 'edit-type': 'input',"global-name": 'cabinet_order'}
            },
            {
                'q': 'cabinet_num',
                'title': '机柜号',
                'display': True,
                'text': {'content': "{n}", 'kwargs': {'n': "@cabinet_num"}},
                'attrs': {},
            },
            {
                'q': None,
                'title': '操作',
                'display': True,
                'text': {'content': "<a href='/assetdetail-{m}.html'>{n}</a>", 'kwargs': {'n': '查看详细','m': '@id'}},
                'attrs': {},
            }
    ]
    q_list=[]
    for i in table_config:
        if not i['q']:
            continue
        q_list.append(i['q'])
    from web import models
    data_list=models.Asset.objects.all().values(*q_list)
    data_list=list(data_list)

    ret={
        'table_config':table_config,
        'data_list':data_list,
         'global_dict': {
                'device_type_choices': models.Asset.device_type_choices,
                'device_status_choices': models.Asset.device_status_choices,
                'idc_choices':list(models.IDC.objects.values_list('id','name'))
            }
    }
    return HttpResponse(json.dumps(ret,ensure_ascii=False))